package com.example.avmanagerweb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.avmanagerweb.Utils.String.StringUtils;
import com.example.avmanagerweb.entity.Gpsqry;
import com.example.avmanagerweb.entity.vo.GpsTrackJson;
import com.example.avmanagerweb.result.RLIST;
import com.example.avmanagerweb.service.GpsqryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2023-10-30
 */
@RestController
@RequestMapping("/gpsqry")
public class GpsqryController {
    @Autowired
    private GpsqryService gpsqryService;

    @PostMapping("/gpstrack")
    public RLIST gpsTrack(@RequestBody GpsTrackJson gpsTrackJson) {
        if (gpsTrackJson.getId() == null || gpsTrackJson.getId() == 0) {
            return RLIST.error().message("参数异常");
        }
        //获取当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式(年-月-日-时-分-秒)
        String createTime = dateFormat.format(now);//格式化然后放入字符串中
        //开始时间
        String begin = gpsTrackJson.getBegin();
        //结束时间
        String end = gpsTrackJson.getEnd();
        Page<Gpsqry> page = new Page<>(gpsTrackJson.getPageNo(), gpsTrackJson.getPageSize());
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("dev_idx", gpsTrackJson.getId());
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("ReceiveDate", begin);
        } else {
            String begin2 = createTime + " 00:00:00.000";
            wrapper.ge("ReceiveDate", begin2);
        }
        //上传结束时间
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("ReceiveDate", end);
        } else {
            String end2 = createTime + " 23:59:59.999";
            wrapper.le("ReceiveDate", end2);

        }
        wrapper.orderByDesc("ReceiveDate");
        gpsqryService.page(page, wrapper);
        long total = page.getTotal();
        List<Gpsqry> list = page.getRecords();
        long numPage = 0;
        if (total == 0) {
            numPage = 1L;
        } else if (list.size() == 0) {
            numPage = 1L;
        } else {
            numPage = (long) Math.ceil(total / (double) gpsTrackJson.getPageSize());
        }
        return RLIST.ok().data("data", list).data("total", total).data("numPage", numPage);
    }
}

